﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using br.com.SistemaChamados;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.Enum;

public partial class Pages_SectorMain : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDdlState();
            FillData();
        } 
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

    }

    private void FillDdlState()
    {
        UnitController unitController = new UnitController();
        ddlUnit.DataSource = unitController.GetUnits();
        ddlUnit.DataTextField = "Name";
        ddlUnit.DataValueField = "Id";
        ddlUnit.DataBind();
        ddlUnit.Items.Insert(0, "Selecione");
    }

    private void MatchDdlUnitValue(Sector sector)
    {
        ListItem listUnit = ddlUnit.Items.FindByValue(Convert.ToString(sector.Unit.Id));

        for (int i = 0; i < ddlUnit.Items.Count; i++)
        {
            ddlUnit.Items[i].Selected = false;
        }
        if (listUnit != null)
        {
            listUnit.Selected = true;
        }
        else
        {
            ddlUnit.Items[0].Selected = true;
        }
    }

    private void FillData()
    {
        string id = Session["SectorIdToUpdate"].ToString();
        Sector sector = new Sector();
        SectorController sectorController = new SectorController();
        sector.Id = Convert.ToInt32(id);
        sector = sectorController.GetSector(sector);
        try
        {
            txbDescription.Text = Convert.ToString(sector.Description);
            MatchDdlUnitValue(sector);
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro no carregamento dos valores: <br>" + ex.Message +
                               ".');</script>");
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Sector sector = new Sector();
        SectorController sectorController = new SectorController();
        br.com.SistemaChamados.Model.Unit unit = null;
        try
        {
            unit = new br.com.SistemaChamados.Model.Unit();
            unit.Id = Convert.ToInt32(ddlUnit.SelectedValue);
            sector.Id = Convert.ToInt32(Session["SectorIdToUpdate"]);
            sector.Description = Convert.ToString(txbDescription.Text);
            sector.Unit = unit;
            if (sectorController.UpdateSector(sector) == EnumGenericState.GENERIC_STATE_SUCCESS)
            {
                Response.Write(@"<script language='javascript'>alert('Setor alterado com sucesso!');</script>");
                Response.Redirect("SectorMain.aspx");
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('Ocorreu um erro durante a alteração, por favor tente novamente!');</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro ao alterar o setor: <br>" + ex.Message + ".');</script>");
            sector = null;
            sectorController = null;
        }
    }
}